package inc
{
	import mx.controls.Image;
	import mx.effects.Move;
	import mx.events.TweenEvent;
	
	public class MovedImage
	{
		private var posx:Array = null;
		private var posy:Array = null;
		private var index:int = 0; //当前点
		private var nextIndex:int = index+1; //下一点
		private var startDelay:int = 500;  //移动延迟时间
		private var moveEffect:Move;
		private var img:Image = null;
		
		public function MovedImage(image:Image, xarray, yarray, index:int, effectDuration:int, startDelay:int)
		{
			this.img = image;
			this.posx = xarray;
			this.posy = yarray;
			this.index = index;
			this.startDelay = startDelay;
			if(moveEffect == null){
				moveEffect = new Move();
			}
			moveEffect.duration = effectDuration;
			moveEffect.startDelay = startDelay;
			moveEffect.target = img;
			moveEffect.addEventListener(TweenEvent.TWEEN_END, tweenEnd);
		}
		
		private function tweenEnd(e:TweenEvent):void{
			index ++;
			move();
		}
		
		public function move():void{
			nextIndex = index + 1;
			if(index >= posx.length){
				index = 0;
			}
			if(nextIndex >= posx.length){
				nextIndex = 0;
			}
			moveEffect.xFrom = posx[index];
			moveEffect.xTo = posx[nextIndex];
			moveEffect.yFrom = posy[index];
			moveEffect.yTo = posy[nextIndex];
			moveEffect.play();
		}
	}
}